package com.pureut.storage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pureut.common.core.utils.bean.BeanUtils;
import com.pureut.storage.domain.order.domain.CustomerManagement;
import com.pureut.storage.dto.CustomerManagementMqDto;
import com.pureut.storage.mapper.CustomerManagementMapper;
import com.pureut.storage.service.CustomerManagementService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/***
 *
 * Author:C
 * Date:2023/03/21 10:58
 * @DESC
 */
@Service
public class CustomerManagementServiceImpl extends ServiceImpl<CustomerManagementMapper, CustomerManagement> implements CustomerManagementService {

    @Resource
    CustomerManagementMapper customerManagementMapper;

    /**
     * 新增
     *
     * @param customerManagementMqDto
     * @return
     */
    @Override
    public boolean insertCustomerManagement(CustomerManagementMqDto customerManagementMqDto) {
        if (customerManagementMqDto.getCustomerNum() != null) {
            CustomerManagement customerManagement1 = customerManagementMapper.selectOne(new QueryWrapper<CustomerManagement>().lambda().eq(CustomerManagement::getCustomerNum, customerManagementMqDto.getCustomerNum()));
            if (customerManagement1 == null) {
                CustomerManagement customerManagement = new CustomerManagement();
                BeanUtils.copyProperties(customerManagementMqDto, customerManagement);
                return save(customerManagement);
            }
        }
        return false;
    }

    /**
     * 修改
     *
     * @param customerManagementMqDto
     * @return
     */
    @Override
    public boolean updateCustomerManagement(CustomerManagementMqDto customerManagementMqDto) {
        CustomerManagement customerManagement = customerManagementMapper.selectOne(new QueryWrapper<CustomerManagement>().lambda().eq(CustomerManagement::getCustomerNum, customerManagementMqDto.getCustomerNum()));
        BeanUtils.copyProperties(customerManagementMqDto, customerManagement);

        return updateById(customerManagement);
    }

    @Override
    public void deleteCustomerManagement(CustomerManagementMqDto customerManagementMqDto) {
        if (customerManagementMqDto.getCustomerNum() != null) {
            List<String> codes = Arrays.asList(customerManagementMqDto.getCustomerNum().split(","));
            List<CustomerManagement> customerManagements = customerManagementMapper.selectList(new QueryWrapper<CustomerManagement>().lambda().in(CustomerManagement::getCustomerNum, codes));
            if (customerManagements.size() > 0) {
                removeByIds(customerManagements);
            }
        }
    }
}
